package com.terrace.core.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * 正则工具
 * @author jiangyg
 *
 */
public class RegularUtils {
	
	/**
	 * 正则匹配验证
	 * @param regex		正则表达式
	 * @param input		输入字符串
	 * @return
	 */
	public static boolean matches(String regex, String input) {
		return Pattern.matches(regex, input);
	}
	
	/**
	 * 通过正则表达式提取{}中的内容
	 * @param str
	 * @return
	 */
	public static List<String> getPropertysByBrace(String str) {
		List<String> propertys = new ArrayList<String>();
		Pattern pattern = Pattern.compile("(?<=\\{)(.+?)(?=\\})");
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()) {
			propertys.add(matcher.group(1).trim());
		}
		return propertys;
	}
	
	/**
	 * 正则判断字符串是否是数值
	 * @param str
	 * @return
	 */
	public static boolean verifyNumber(String str) {
		if (StringUtils.isNotBlank(str)) {
			String reg = "^\\d+\\.{0,1}\\d+$";
			return Pattern.compile(reg).matcher(str).find();
		}
		return false;
	}
	
	/**
	public static String replacePropertyByBrace() {
		String regex = "第[0-9]*条";
		String str = "第9条,数据错误,错误信息,第jjj哦条哦条我的条件如何？第221条xx";
		Pattern pat = Pattern.compile(regex);
		Matcher matcher = pat.matcher(str);
		while (matcher.find()) {
			String temp = str.substring(matcher.start(), matcher.end());
			str = str.replaceAll(temp, temp.substring(0, temp.lastIndexOf("条")) + "行");
		}
		System.out.println(str);
		return str;
	}
	**/

}
